﻿using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace BLE_Keypad_Tool_Controls
{
	[DefaultEvent("Click")]
	public partial class Key : UserControl
	{
		private const float one_unit_of_size = 13.5F; // 每 0.25 个宽度对应的像素数
		private float key_width = 1.0F; // 默认键盘宽度（1U），以及固定键盘高度
		private string legend = "";

		public Key()
		{
			InitializeComponent();

			SetStyle(ControlStyles.AllPaintingInWmPaint, true);
			SetStyle(ControlStyles.UserPaint, true);

			KeyWidth = key_width;
		}

		#region 控件属性
		[Category("外观")]
		[Browsable(false)]
		public int KeyWidthPixel
		{
			get { return (int)(key_width / 0.25F * one_unit_of_size); }
		}

		[Category("外观")]
		[Browsable(false)]
		public int KeyHeightPixel
		{
			get { return (int)(4 * one_unit_of_size); }
		}

		[Category("外观")]
		[Browsable(true)]
		[Description("按键宽度，单位（U），步长 0.25")]
		public float KeyWidth
		{
			get { return key_width; }
			set
			{
				key_width = value;

				if (key_width < 0.5F || key_width > 24.0F || key_width * 100 % 25 != 0)
				{
					key_width = 1.0F;
				}

				Size = new Size(KeyWidthPixel, KeyHeightPixel);

				Invalidate();
			}
		}

		[Category("外观")]
		[Browsable(true)]
		[Description("按键名称")]
		public string Legend
		{
			get { return legend; }
			set
			{
				legend = value;
				Invalidate();
			}
		}

		[Category("数据")]
		[Description("索引序号")]
		public int Index { get; set; } = 0;

		[Category("数据")]
		[Description("硬件定义的索引序号")]
		public int KeyIndex { get; set; } = 0;

		[Category("数据")]
		[Description("键码值")]
		public int KeyCode { get; set; } = 0;
		#endregion

		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);

			BufferedGraphicsContext buffer_context = BufferedGraphicsManager.Current;
			BufferedGraphics buffer = buffer_context.Allocate(e.Graphics, e.ClipRectangle);
			Graphics graphics = buffer.Graphics;

			graphics.Clear(SystemColors.Control);

			graphics.SmoothingMode = SmoothingMode.HighQuality;

			DrawRoundedRect(
				graphics,
				new SolidBrush(Color.FromArgb(0xCC, 0xCC, 0xCC)),
				new Pen(Color.Black, 1),
				new Rectangle(0, 0, Width - 1, Height - 1),
				8
			);

			DrawRoundedRect(
				graphics,
				new SolidBrush(Color.FromArgb(0xFB, 0xFB, 0xFB)),
				new Pen(Color.FromArgb(0xB7, 0xB7, 0xB7), 1),
				new Rectangle(6, 3, Width - 13, Height - 13),
				3
			);

			graphics.DrawString(
				legend,
				new Font(Font.Name, 12, FontStyle.Regular, GraphicsUnit.Pixel),
				new SolidBrush(Color.Black),
				8, 4);

			buffer.Render(e.Graphics);
			buffer.Dispose();
			graphics.Dispose();
		}

		private void DrawRoundedRect(Graphics g, Brush fill, Pen pen, Rectangle rect, int radius)
		{
			GraphicsPath gp = new GraphicsPath();

			gp.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
			gp.AddArc(rect.X + rect.Width - radius, rect.Y, radius, radius, 270, 90);
			gp.AddArc(rect.X + rect.Width - radius, rect.Y + rect.Height - radius, radius, radius, 0, 90);
			gp.AddArc(rect.X, rect.Y + rect.Height - radius, radius, radius, 90, 90);
			gp.CloseAllFigures();

			g.FillPath(fill, gp);
			g.DrawPath(pen, gp);
		}
	}
}
